﻿using System;
using System.Runtime.InteropServices;

namespace LDSModel.Item
{
    /// <summary>
    /// 日期结构体。
    /// </summary>
    [StructLayout(LayoutKind.Explicit, Size = 4)]
    public class STU_Date
    {
        [FieldOffset(0)]
        public UInt16 Year = 0;

        [FieldOffset(2)]
        public byte Month = 0;

        [FieldOffset(3)]
        public byte Day = 0;

        public DateTime ProductionDate
        {
            get
            {
                return new DateTime(Year, Month, Day);
            }
            set
            {
                Year = Convert.ToUInt16(value.Date.Year & 0xFF);
                Month = Convert.ToByte(value.Date.Month & 0xFF);
                Day = Convert.ToByte(value.Date.Day & 0xFF);
            }
        }
    }
    
    [StructLayout(LayoutKind.Explicit, Size = 4)]
    public class STU_SYSCFG
    {
        [FieldOffset(0)]
        public UInt16 LocalAddr = 0;				    // 源地址,本机地址

        [FieldOffset(2)]
        public UInt16 DestAddr = 0;				        // 中心地址,目标地址

        [FieldOffset(4)]
        public UInt16 BaseStationAddr = 0;		        // 基站地址，如果本身是基站与LocalAddr相同

        [FieldOffset(6)]
        public UInt16 IsBaseStation = 0;			    // 是否基站
    }

    /// <summary>
    /// 系统信息结构体。
    /// </summary>
    [StructLayout(LayoutKind.Explicit)]
    public class SysInfoItem
    {
        [FieldOffset(0)]
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
        private byte[] _ID = new byte[8];

        [FieldOffset(8)]
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 12)]
        private byte[] _Name = new byte[12];

        [FieldOffset(20)]
        private UInt16 _PrimaryVer;

        [FieldOffset(22)]
        private UInt16 _AuxVer;

        [FieldOffset(24)]
        [MarshalAs(UnmanagedType.Struct, SizeConst = 4)]
        STU_Date _ProductionDate = new STU_Date();

        [FieldOffset(28)]
        [MarshalAs(UnmanagedType.Struct, SizeConst = 8)]
        STU_SYSCFG _Config = new STU_SYSCFG();

        public string ID
        {
            get
            {
                string id = System.Text.ASCIIEncoding.Default.GetString(_ID);
                return id;
            }
            set
            {
                if (value.Length != 8)
                {
                    throw new Exception("ID的长度必须为8位。");
                }
                byte[] bytes = System.Text.ASCIIEncoding.Default.GetBytes(value);
                Array.Copy(bytes, _ID, 8);

            }
        }

        public string Name
        {
            get
            {
                string name = System.Text.ASCIIEncoding.Default.GetString(_Name);
                name = name.TrimEnd(new char[] { '\0', ' ' });
                return name.TrimEnd();
            }
            set
            {
                Array.Clear(_Name, 0, _Name.Length);
                byte[] bytes = System.Text.ASCIIEncoding.Default.GetBytes(value);
                Array.Copy(bytes, _Name, bytes.Length);
            }
        }

        public int PrimaryVer
        {
            get
            {
                return Convert.ToInt32(_PrimaryVer);
            }
            set
            {
                _PrimaryVer = Convert.ToUInt16(value);
            }
        }


        public int AuxVer
        {
            get
            {
                return Convert.ToInt32(_AuxVer);
            }
            set
            {
                _AuxVer = Convert.ToUInt16(value);
            }
        }


        public DateTime ProductionDate
        {
            get
            {
                return _ProductionDate.ProductionDate;
            }
            set
            {
                _ProductionDate.ProductionDate = value;
            }
        }

        public int LocalAddr
        {
            get { return _Config.LocalAddr; }
            set { _Config.LocalAddr = Convert.ToUInt16(value); }
        }

        public int DestAddr
        {
            get { return _Config.DestAddr; }
            set { _Config.DestAddr = Convert.ToUInt16(value); }
        }

        public int BaseStationAddr
        {
            get { return _Config.BaseStationAddr; }
            set { _Config.BaseStationAddr = Convert.ToUInt16(value); }
        }

        public bool IsBaseStation
        {
            get
            {
                if (_Config.IsBaseStation > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }

            set
            {
                _Config.IsBaseStation = value ? Convert.ToUInt16(1) : Convert.ToUInt16(0);
            }
        }
    }
}
